﻿<%@page contentType="text/html;charset=utf-8" import="
		java.util.List,java.util.Iterator,java.math.BigDecimal,java.util.Properties,
		java.util.ArrayList,java.util.HashMap,java.util.Map,java.util.StringTokenizer,
		org.json.simple.JSONObject,org.json.simple.JSONArray,com.innoace.mo.sql.*,
		com.innoace.mwp.MwpObject,com.innoace.mwp.MwpArray,com.innoace.mwp.MwpContext,
		com.innoace.mo.MoAction,com.innoace.mo.MoException,com.innoace.mo.MoFormat,
		org.apache.log4j.Logger,java.io.PrintWriter,sun.misc.*,java.net.*,java.io.*,
		com.innoace.mwp.*,java.util.*,java.text.*,org.apache.http.*,java.sql.*,org.json.simple.parser.*,com.innoace.mwp.MwpPlatform,com.innoace.mo.iss.*, com.innoace.mo.sql.vote.*
	"
%><%!
	public boolean canVoteForSpecial(MwpContext ctx, HashMap<String, String> hmParam){
		boolean canVote = false;
		try{			
			String sVoteSeq = hmParam.get("voteSeq");
			String sSwbeonho = hmParam.get("swbeonho");	

			java.sql.Connection con = ctx.con;						

			Vote vote = new Vote();
			vote.voteSeq = sVoteSeq;
			vote.mdn = sSwbeonho;			

			List<Vote> list = ctx.mapper(VoteMapperOracle.class).canVoteForSpecial(vote);

			if(list != null && list.size() > 0){
				canVote = true;
			}else{
				canVote = false;
			}

			con.commit();
			con.close();
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			return canVote;		
		}
	}	
	
	//SSO 연동 결과 확인
	public String canVoteForEmp(HashMap<String, String> hmParam){
    URL url = null;
    URLConnection urlConnUtf = null;
    BufferedReader readerUtf = null;
    String resultUtf = "";
		String returnStr = "";
    try {

			String sId = hmParam.get("id");			
			String sPwd = hmParam.get("pw");
		
			String param = "";
			if(sId != null && !sId.equals("")){
				param = "?id=" + sId;
			}
			
			if(sPwd != null && !sPwd.equals("")){
				param += "&pw=" + sPwd;
			}			
	
	    url = new URL("http://172.16.164.103:9050/emp_sf/magicsso/onlyAuth.jsp" + param);
	   
	    String lineUtf = null;
	    
	    urlConnUtf = url.openConnection();
	    urlConnUtf.setUseCaches(false);   
			urlConnUtf.setConnectTimeout(1000);
			urlConnUtf.setReadTimeout(1000);
	         
	    
	    readerUtf = new BufferedReader(new InputStreamReader(urlConnUtf.getInputStream(), "utf-8"));
	    while((lineUtf = readerUtf.readLine()) != null) {
	        resultUtf += lineUtf + "\n";
	    }        

			if(resultUtf != null && resultUtf.trim().length() > 0){
				returnStr = resultUtf.trim();
			}else{
				returnStr	= "-999";
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			return returnStr;
		}
	}
%>

<%
	BASE64Decoder decoder = new BASE64Decoder(); 

	String encId = request.getParameter("encId");	
	String encPwd = request.getParameter("encPwd");	
	boolean canVote = false;
	String alertMsg = "";

	//사번 복호화
	String encIdString = new StringBuffer(encId).toString();
	System.out.println("encIdString : "+encIdString);
	byte[] plainId = decoder.decodeBuffer(encIdString);	
	String decId = new String(plainId, "UTF-8");	
	System.out.println("decId : "+decId);	

	//일반 투표자의 경우 암호 존재
	//임원의 경우 암호 없음
	if(encPwd != null){
		//암호 복호화
		//일반 투표자
		String encPwdString = new StringBuffer(encPwd).toString();
		System.out.println("encPwdString : "+encPwdString);
		byte[] plainPwd = decoder.decodeBuffer(encPwdString);
		String decPwd = new String(plainPwd, "UTF-8");	
		System.out.println("decPwd : "+decPwd);
		
		HashMap<String, String> hmParam = new HashMap<String, String>(); 
		hmParam.put("id", decId);
		hmParam.put("pw", decPwd);			
		
		//SSO 연동
		String resultStr = canVoteForEmp(hmParam);
		
		
		/*
		00001 : 일치
		10001 : 존재하지 않는 사용자 ( ID 틀림 )
		10003 : 패스워드 틀림
		10009 : 패스워드 틀림
		10010 : 계정잠김
		09999 : IO 에러
		*/
		System.out.println("resultStr : "+resultStr);
		
		if(resultStr.equals("00001")){
			canVote = true;
		}else if(resultStr.equals("10001")){
			canVote = false;
			alertMsg = "존재하지 않는 사용자입니다.";
		}else if(resultStr.equals("10003") || resultStr.equals("10009")){			
			canVote = false;
			alertMsg = "비밀번호를 확인하여 주십시오.";
		}else if(resultStr.equals("10010")){
			canVote = false;
			alertMsg = "비밀번호를 3회이상 틀렸습니다.\\n계정이 잠겼습니다.";
		}else if(resultStr.equals("09999")){			
			canVote = false;
			alertMsg = "I/O 오류입니다.";
		}else{
			canVote = false;			
			alertMsg = "RETRY";
		}
		
//		if(decId != null && (decId.equals("20512518") || decId.equals("20512519") || decId.equals("20512520"))){
//			canVote = true;
//		}
				
	}else{
		//임원
		MwpContext ctx = MwpContext.getContext();		
		com.innoace.mo.MoFormat fmt = com.innoace.mo.MoFormat.getFormat();
		try{		
			ctx.init(request, response, out);
			
			HashMap<String, String> hmParam = new HashMap<String, String>(); 
			hmParam.put("voteSeq", "4");
			hmParam.put("swbeonho", decId);						
			
			canVote = canVoteForSpecial(ctx, hmParam);
		}finally{
			ctx.destroy();
			
			alertMsg = "등록번호(2자리)를 확인하여 주십시오.";
		}	
	}
	
	if(canVote){
		out.println("success");
		session.setMaxInactiveInterval(24*60*60);
		session.setAttribute("loginCheck", "true");
		session.setAttribute("swbeonho", decId);
	}else{
		out.println(alertMsg);
	}
%>